﻿#region itdc

/************************************************************************
*Copyright(C) 2015 by qsgy
 *All rights reserved. 
 *FileName:     $safeitemname$
 *Author:       oyh
 *Version:      
 *UnityVersion：
 *Date:   $time$      
 *Description:    
 *History: 
************************************************************************/
#endregion
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 转换条件
/// </summary>
[RequireComponent(typeof(Mover))]
public class SeePlayer : Transition{
    #region show Fields
    [SerializeField, Tooltip("左右视野角度")]
    private float sightField = 60f;
    [SerializeField, Tooltip("视觉距离")]
    private float sightStrength = 4f;
    [SerializeField,Tooltip("视觉模式")]
    private SeeType seeType=SeeType.View;
    [Tooltip("输出为是否在视线内。否则在视线外为true")]
    public bool isInner = true;
    [Tooltip("辅助线颜色")]
    public Color color = Color.blue;
   
    #endregion
    #region hide fields
    Mover mover;
    private Transform selfTrans;
    private Transform playerTransform;//玩家trans

    #endregion

    public SeePlayer()
    {
        id = TransitionID.SeePlayer;

    }

    #region Mono method
    private void Awake()
    {
        selfTrans = this.transform;//获取自身trans。缓存
        mover = GetComponent<Mover>();
    }

    private void Start()
    {
        playerTransform = GameManager.Instance.Sui.transform;//获取玩家trans
#if DEBUG
        if (playerTransform == null) Debug.LogError(name + "未能获取到GameManager的玩家");
#endif
    }

    private void OnDrawGizmos()
    {

        // Gizmos.DrawSphere(transform.position, sightStrength);
        Gizmos.color = color;

        switch (seeType)
        {
            case SeeType.Distance:
                float theta = 0;
                float thetaNext;
                for (int i = 0; i < 21; i++)
                {

                    theta = Mathf.PI*2 *(float)i/21f;
                    thetaNext= Mathf.PI*2 * (float)(i+1) / 21f;
                    float x = transform.position.x;
                    float y = transform.position.y;
                    float z = transform.position.z;
                    Gizmos.DrawLine(new Vector3(x + Mathf.Cos(theta) * sightStrength, y + Mathf.Sin(theta) * sightStrength, z),
                        new Vector3(x + Mathf.Cos(thetaNext)*sightStrength, y + Mathf.Sin(thetaNext)*sightStrength, z));
                    //print("ok");
                }
                break;
            default:
                Vector3 dir = GetComponent<Mover>().tagertPos - transform.position;
                Vector3 cuizhi = Vector3.Cross(dir, new Vector3(0, 0, 1));
                Gizmos.DrawLine(transform.position,transform.position+ (dir+Mathf.Tan(sightField/360f*Mathf.PI)*cuizhi).normalized * sightStrength);
                Gizmos.DrawLine(transform.position, transform.position + (dir - Mathf.Tan(sightField / 360f * Mathf.PI) * cuizhi).normalized * sightStrength);

                break;
        }
       
    }
    #endregion
    #region normal method
    /// <summary>
    /// 是否在视距内
    /// </summary>
    /// <returns></returns>
    public bool InSight()
    {
      //  print("play" + playerTransform.position + "  self" + selfTrans.position);
      //  print((selfTrans.position - playerTransform.position).magnitude <= sightStrength);
        if (playerTransform == null) return false;
        if ((selfTrans.position - playerTransform.position).magnitude <= sightStrength)
            return true;
        return false;
    }

    /// <summary>
    /// 是否在视野角度内
    /// </summary>
    /// <returns></returns>
    public bool InField()
    {
        if (playerTransform == null) return false;
        
        Vector3 pDir = playerTransform.position - selfTrans.position;
        if(Mathf.Abs( Vector3.Angle(pDir, mover.tagertPos-selfTrans.position))<sightField)
        return true;
        return false;
    }

    /// <summary>
    /// 在视觉内。既在视距内又在视野角度内
    /// </summary>
    /// <returns></returns>
    public bool InView()
    {
       // print("dis" + InSight() + ", field" + InField());
        return InSight() & InField();
    }

    /// <summary>
    /// 是否转换
    /// </summary>
    /// <returns></returns>
    public override bool IsTransition()
    {
        bool result = false;
        switch (seeType)
        {
            case SeeType.Field:
                result= InField();
                break;
            case SeeType.Distance:
                result= InSight();
                break;
            case SeeType.View:
                result = InView();
                break;
            default:
                result = false;
                 break;
        }
        if (!isInner) result = !result;
        return result;
    }

    
    #endregion
    #region SeeType

    /// <summary>
    /// 视觉模式。Field视野角度，Distance视距，View视觉前两者之交集
    /// </summary>
    public enum SeeType
    {
        Field, Distance, View
    } 
    #endregion
}
